<?php
namespace admin;
use models\BaseDao;
use Slince\Upload\UploadHandlerBuilder;
use JasonGrimes\Paginator;
class Ask extends Admin{
    public function  __construct(){
        parent::__construct();
        $this->assign('menumark','ask');
        
    }
/**
 * 咨询列表页面
 */
    function index(){
        // 获取数据库操作对象
        $db = new BaseDao();

        // 分页
        $num = $_GET['num'] ?? 1;

        $prosql['ORDER'] = ['id'=>'DESC'];
        $where = [];


        if(isset($_GET['state']) && $_GET['state'] !=''){
            $where['ask.state'] = $_GET['state'];
            $state = '&state='.$_GET['state'];
        }

        if(!empty($_GET['name'])){
            $where['product.name[~]'] = $_GET['name'];
            $name = '&name='.$_GET['name'];
        }
        if(!empty($_GET['asktext'])){
            $where['ask.asktext[~]'] = $_GET['asktext'];
            $asktext = '&asktext='.$_GET['asktext'];
        }
        if(!empty($_GET['uname'])){
            $where['ask.uname[~]'] = $_GET['uname'];
            $uname = '&uname='.$_GET['uname'];
        }
    
        

        $this->assign('get',$_GET);


        $totalItems = $db->count('ask',['[>]product'=>['pid'=>'id']],'*',$where);
       
        $itemsPerPage = PNUM;
        $currentPage = $num;
        $urlPattern = '/admin/ask?num=(:num)'.$state.$name.$asktext.$uname;

        $paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

        $start = ($currentPage-1) * $itemsPerPage;
        $prosql['LIMIT'] = [$start,$itemsPerPage];
        $prosql = array_merge($prosql,$where);

        // 获取全部咨询,按照ord排序
        $data = $db->select('ask',
        ['[>]product'=>['pid'=>'id']],
        ['ask.id','ask.pid','ask.uid','ask.uip',
        'ask.atime','ask.uname','ask.replytext','ask.replytime','ask.state','ask.asktext',
    'product.name','product.logo']
        ,$prosql);
        // 将数据全部分给模板
        $this->assign('data',$data);
        $this->assign('fpage',$paginator);

        //标题    
        $this->assign('title','咨询列表');
        // 导入模板
        $this->display('ask/index');
    }
 
    function reply($id){
        $db = new BaseDao();

        $data = $db->get('ask',
        ['[>]product'=>['pid'=>'id']],
        ['ask.id','ask.pid','ask.uid','ask.uip',
        'ask.atime','ask.uname','ask.replytext','ask.replytime','ask.state','ask.asktext',
    'product.name','product.logo'],
['ask.id'=>$id]);

        $this->assign($data);

        $this->assign('title',"回复咨询");
        $this->display('ask/reply');
    }



    function doreply(){
        $id = $_POST['id'];
        unset($_POST['id']);
        

        $db = new BaseDao();


        if(!empty($_POST['replytext'])){
            $_POST['replytime'] = time();
            $_POST['state'] = 1;
        }else{
            $_POST['replytime'] =  $_POST['state'] = 0;
        }

        if($db->update('ask',$_POST,['id'=>$id])){
            $this->success('/admin/ask?state=1','回复ok');
        }else{
            $this->error('/admin/ask/reply/'.$id,'回复fail');
        }
    }
    function del($id){
        $db = new BaseDao();
        if($db->delete('ask',['id'=>$id])){
            $this->success('/admin/ask','咨询删除ok');
        }else{
            $this->error('/admin/ask','删除fail');
        }
    }
    function alldel(){
        $db = new BaseDao();
        $num = 0;
        foreach($_POST['id'] as $id){
            
            $num += $db->delete('ask',['id'=>$id]);
        }
        if($num > 0){
            $this->success('/admin/ask',$num.'个咨询删除ok');
        }else{
            $this->success('/admin/ask','删除失败');
        }
    }
}